/**
 * 格式化日期格式
 * @param {Date} date
 * @param {string} datetype
 * @param {string} formattype
 */
export const kydFormatDate = (date: any, datetype: string, formattype: string) => {
    if (!date) return '';
    if (typeof date == 'number') {
        date = new Date(date);
    }
    if (typeof date == 'string') {
        return date;
    }
    const tYear = date.getFullYear();
    let tMonth = date.getMonth() + 1;
    tMonth = tMonth.toString().length === 1 ? `0${tMonth}` : `${tMonth}`;
    let tDate = date.getDate();
    tDate = tDate.toString().length === 1 ? `0${tDate}` : tDate;
    let tHours = date.getHours();
    tHours = tHours.toString().length === 1 ? `0${tHours}` : tHours;
    let tmm = date.getMinutes();
    tmm = tmm.toString().length === 1 ? `0${tmm}` : tmm;
    let ts = date.getSeconds();
    ts = ts.toString().length === 1 ? `0${ts}` : ts;
    if (datetype === 'month') {
        return `${tYear}${formattype}${tMonth}`;
    }
    if (datetype === 'day') {
        return `${tYear}${formattype}${tMonth}${formattype}${tDate}`;
    }
    if (datetype === 'times') {
        return `${tYear}${formattype}${tMonth}${formattype}${tDate} ${tHours}:${tmm}`;
    }
    if (datetype === 'time') {
        return `${tYear}${formattype}${tMonth}${formattype}${tDate} ${tHours}:${tmm}:${ts}`;
    }
};


/**
 * 全角转半角
 * @param str 
 */
export const kydToCDB = (str: string) => {
    let tmpStr = '';
    for (let i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) === 12288) {
            tmpStr += String.fromCharCode(str.charCodeAt(i) - 12256);
            continue;
        }
        if (str.charCodeAt(i) > 65280 && str.charCodeAt(i) < 65375) {
            tmpStr += String.fromCharCode(str.charCodeAt(i) - 65248);
        } else {
            tmpStr += String.fromCharCode(str.charCodeAt(i));
        }
    }
    return tmpStr;
};

/**
 * 校验是否合法的18位身份证
 * @param {string} idcode
 */
export const kydCheckIDCard = (idcode: string | number) => {
    if (!idcode || (idcode+'').length != 18) return false;
    idcode = kydToCDB(idcode + '');
    const weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    const check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    const code = idcode + '';
    const last = idcode[17];
    const seventeen = code.substring(0, 17);
    // 判断最后一位校验码是否正确
    const arr:Array<any> = seventeen.split('');
    const len = arr.length;
    let num = 0;
    for (let i = 0; i < len; i++) {
        num = num + arr[i] * weight_factor[i];
    }
    // 获取余数
    const resisue = num % 11;
    const last_no = check_code[resisue];
    const idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X]|[x])$/;
    // 判断格式是否正确
    const format = idcard_patter.test(idcode);
    // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
    return !!(last.toLowerCase() == last_no.toLowerCase() && format);
};

/**
 * 判断是否合法的中文姓名
 * @param name 
 */
export const kydNameReg = (name: string) => {
    const reg = /^[a-zA-Z\u4E00-\u9FA5\uF900-\uFA2D\u00B7\u2022\u0095\u0387]+$/;
    const strRe = /[\u4E00-\u9FA5]/g;
    const str = name.match(strRe);
    let strlength;
    if (str == null) {
        strlength = name.length;
    } else {
        strlength = name.length + str.length * 2; // 汉字按三个字节
    }
    if (strlength <= 3 || strlength > 48) {
        return false;
    }
    if (!name.match(reg)) {
        return false;
    } else {
        const headExp = /^[\u00B7\u2022\u0095\u0387]+/;
        const tailExp = /[\u00B7\u2022\u0095\u0387]+$/;
        const zhExp = /[\u4E00-\u9FA5\uF900-\uFA2D]+[\s]+/;
        const zhcharExp = /[\u4E00-\u9FA5\uF900-\uFA2D]+[u00B7\u2022\u0095\u0387]?[a-zA-Z]+/;
        const charzhExp = /[a-zA-Z]+[u00B7\u2022\u0095\u0387]?[\u4E00-\u9FA5\uF900-\uFA2D]+/;
        if (headExp.test(name)) {
            return false;
        }
        if (tailExp.test(name)) {
            return false;
        }
        if (zhExp.test(name)) {
            return false;
        }
        if (zhcharExp.test(name)) {
            return false;
        }
        if (charzhExp.test(name)) {
            return false;
        }
        return true;
    }
};


/**
 * 判断是否是合法的11位手机号
 * @param {string} phone
 */
export const kydCheckPhone = (phone: number | string) => {
    const phoneTest = /^((13[0-9])|(14[1|4|5|6|7|8|9])|(15([0|1|2|3|5|6|7|8|9]))|(16[2|5|6|7])|(17[0|1|2|3|5|6|7|8])|(18[0-9])|(19[1|8|9]))\d{8}$/;
    return phoneTest.test(phone+'');
};

/**
 * 身份证脱敏
 * @param id
 */
export const kydFormatIdCard = (str:string, frontLen:number, endLen:number) => {
    const len = str.length - frontLen - endLen;
    let xing = '';
    for (let i = 0; i < len; i++) {
        xing += '*';
    }
    return (
        str.substring(0, frontLen) + xing + str.substring(str.length - endLen)
    );
};

/**
 * 手机号脱敏
 * @param str 
 */
export const kydFormatPhone = (str:string | number) => {
    str = str + ''
    return str.substring(0, 3) + '******' + str.substring(str.length - 2);
};

/**
 * 姓名脱敏
 * @param str
 */
export const kydFormatName = (str:string) => {
    if (null != str && str != undefined) {
        if (str.length <= 3) {
            return '*' + str.substring(1, str.length);
        } else if (str.length > 3 && str.length <= 6) {
            return '**' + str.substring(2, str.length);
        } else if (str.length > 6) {
            return str.substring(0, 2) + '****' + str.substring(6, str.length);
        }
        return '*' + str.substring(1, str.length);
    } else {
        return '';
    }
};

/**
 * 判断是安卓还是ios
 */
export const kydIsAndroidOrIos = () => {
    const u = navigator.userAgent;
    const isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    if (isIos) {
        return true;
    }
    if (isAndroid) {
        return false;
    }
};

/**
 * 判断是否iphonex
 */
export const kydIsIphoneX = () => {
    if (/iphone/gi.test(window.navigator.userAgent)) {
        const x = window.screen.width === 375 && window.screen.height === 812;
        const xsMax =
            window.screen.width === 414 && window.screen.height === 896;
        const xR = window.screen.width === 414 && window.screen.height === 896;
        if (x || xsMax || xR) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
};